﻿@{
    ViewBag.Title = "流向表";
    Layout = "~/Views/Shared/_ManufactureList.cshtml";
}
<link href="~/Content/css/container_report_print.css" rel="stylesheet" media="print" />
<style type="text/css">
    .print {
        display: none;
    }

    .tool_c {
        padding: 10px 0;
    }

    .btn-group {
        padding: 0 10px 0 0;
    }

    
    .grid_list_1_c table tr[id] td:nth-child(9) {
        background-color: #ffeeb5;
    }

    .grid_list_1_c table tr[id] td:nth-child(10) {
        background-color: #00cfd1;
        color: #fff;
    }

    .btn-group:last-child a.btn:first-child {
        margin: 0 0.5em 0 0 !important;
    }

    .input_inline input {
        display: inline-block;
        width: 4em;
    }

    .btn-group input[type=text] {
        height: 2em !important;
    }
</style>
<script>
    $(function () {
        Index.init();
    });

    var Index = {
        defaultDate:@Html.Raw(ViewData["LedgerDate"].ToString()),
        printTemplate: null,
        titlies: {
            "0": "{0}流向收发存统计表",
            "1": "{1}流向收发存汇总表"
        },
        curUnit: '@ViewData["curUnit"].ToString()',
        curUserName: '@ViewData["curUserName"].ToString()',
        curDate: '@ViewData["curDate"].ToString()',
        curTime: '@ViewData["curTime"].ToString()',
        dataList: {
            "0": null,
            "1": null
        },
        printItemTemplate: "",
        init: function () {
            with (Index) {
                printItemTemplate = $("#print1 .page").html();

                initControl();

                loadDataList();
            }
        },
        initControl: function () {
            $("[date-control]").val(Index.defaultDate);

            $("[date-control]").on("click", function () {
                WdatePicker({ dateFmt: 'yyyyMM', minDate: '2000-1', maxDate: '2050-12' });
            });

            $('select').bindSelect();

            $("#FHappenCompanyNO2").bindSelect({
                url: "/WareHouse/Container/GetSupplierJson",
                id: "UnitNO",
                text: "UnitNOShortName"
            });
            $("#FHappenCompanyNO1").bindSelect({
                url: "/WareHouse/Container/GetHappenCompanyInfo",
                id: "UnitNO",
                text: "UnitShortNOName"
            });
            //批次查询 btn_search1
            $("#btn_search1").on("click", function () {
                var $gridList1 = $("#gridList1");

                var conn1 = $.buildPostParam("FAccountDate,FHappenCompanyNO,FContainerNO", "1");

                $gridList1.jqGrid('setGridParam', {
                    postData: conn1,
                }).trigger('reloadGrid');
            });
            //批次查询 btn_search2
            $("#btn_search2").on("click", function () {
                var $gridList2 = $("#gridList2");

                var conn2 = $.buildPostParam("FHappenCompanyNO,FContainerNO", "2");

                $gridList2.jqGrid('setGridParam', {
                    postData: conn2,
                }).trigger('reloadGrid');
            });
            //打印      
            $("#butnPrint1").on("click", function () {
                $("#print1").jqprint();
            });
            $("#butnPrint2").on("click", function () {
                $("#print2").jqprint();
            });
            //导出
            $("#butnExport1").on("click", function () {
                var param = { id: "V_RQK_32_LX_StoConAIAOAcc", param: Index.buildExportConn("FAccountDate,FHappenCompanyNO,FContainerNO".split(","), "1") };

                var url = "/Common/ExcelExportById?{0}".format($.param(param));

                location.href = url;
            });
            $("#butnExport2").on("click", function () {
                var columnInfo = [
                                { caption: '发生单位', name: 'FHappenCompanyNO' },
                                { caption: '发生单位名称', name: 'FHappenCompanyName' },
                                { caption: '容器分类名称', name: 'FContainerTypeName' },
                                { caption: '容器编号', name: 'FContainerNO' },
                                { caption: '容器名称', name: 'FContainerName' },
                                { caption: '库存数量', name: 'KCZS' },
                                { caption: '期初数量', name: 'QC' },
                                { caption: '空箱入库', name: 'KRK' },
                                { caption: '空箱出库', name: 'KCK' },
                                { caption: '空箱报废', name: 'KBS' },
                                { caption: '空箱返修', name: 'KFX' },
                                { caption: '空箱盘存', name: 'KPC' },
                                { caption: '空箱库存', name: 'KKC' },
                                { caption: '重箱入库', name: 'ZRK' },
                                { caption: '重箱出库', name: 'ZCK' },
                                { caption: '重箱库存', name: 'ZKC' }
                ];

                var p = $.param(Index.buildSql("FHappenCompanyNO,FContainerNO".split(","), "2", columnInfo, "V_RQK_32_LX_StoConAIAOAcc_ZKHZ"));

                var url = "/Common/ExcelExport?{0}".format(p);

                location.href = url;
            });
            $("#gridList1").delegate("a[明细]","click",function(){
                var num = $.trim($(this).parents("tr").find("td:first").text());
                
                var r = Index.dataList["0"].rows[num-1],rs={};

                var cols = "FAccountDate,FHappenCompanyNO,FContainerNO,FContainerName,FHappenCompanyNOName".split(",");

                cols.forEach(function(col){
                    col in r &&  (rs[col] = r[col]);
                
                });
                
                var url = "/WareHouse/Container/ContainerReportDetails?{0}".format($.param(rs));

                location.href = url;
            });
        },
        buildExportConn: function (params, num) {
            var conn = "";

            !num && (num = "");

            params.forEach(function (item) {
                var val = $("#{0}{1}".format(item, num)).val();

                !!val && (conn += ("and {0} = '{1}'".format(item, val)));
            });

            return conn;
        },
        loadDataList: function () {
            var conn1 = $.buildPostParam("FAccountDate,FHappenCompanyNO,FContainerNO", "1");

            var $gridList1 = $("#gridList1");///BOM/Blankingcard/GetBlankingcards

            var buildOperate = function (cellvalue, options, rowObject) {
                var template = "<a href=\"javascript:void(0);\" 明细>明细</a>";

                return template;
            }

            $gridList1.dataGridForJson({
                url: "/WareHouse/Container/GetMoveToReceiveAndDispatchStatistics",
                postData: conn1,
                height: $(window).height() - 240,
                colModel: [
                { label: '操作', name: 'FContainerNO', width: 60, align: 'center', formatter: buildOperate},
                { label: '仓库单位', name: 'FHappenCompanyNO', width: 100, align: 'center', sortable: false },
                { label: '仓库单位名称', name: 'FHappenCompanyName', width: 140, align: 'left', sortable: false },
                { label: '容器编号名称', name: 'FHappenCompanyNOName', width: 80, align: 'center', sortable: false, hidden: true },
                { label: '台账日期', name: 'FAccountDate', width: 80, align: 'center', sortable: false, hidden: true },
                { label: '容器编号', name: 'FContainerNO', width: 80, align: 'center', sortable: false, hidden: true },
                { label: '容器名称', name: 'FContainerName', width: 140, align: 'left', sortable: false },
                { label: '重箱<br/>库存', name: 'ZXKC', width: 80, align: 'right', sortable: false },
                { label: '空箱<br/>库存', name: 'KXKC', width: 80, align: 'right', sortable: false },
                { label: '库存<br/>数量	', name: 'KC', width: 80, align: 'right', sortable: false },
                { label: '期初<br/>数量	', name: 'FQuantity_QC', width: 80, align: 'right', sortable: false },
                { label: '入库<br/>数量	', name: 'FQuantity_RK', width: 60, align: 'right', sortable: false },
                { label: '出库<br/>数量', name: 'FQuantity_CK', width: 60, align: 'right', sortable: false },
                { label: '废品<br/>数量', name: 'FQuantity_BS', width: 60, align: 'right', sortable: false, },
                { label: '返修<br/>数量', name: 'FQuantity_FX', width: 60, align: 'right', sortable: false, },
                { label: '盈亏<br/>数量', name: 'FQuantity_PC', width: 60, align: 'right', sortable: false },
                 { label: '', name: '', width: $(window).width() - 1120, align: 'left' }
                ],
                pager: "#gridPager1",
                sortname: 'FHappenCompanyNO,FContainerNO',
                sortorder: "asc,asc",
                viewrecords: true,
                rownumWidth: 50,
                onSelectRow: function (rowId) {

                },
                loadComplete: function (data) {
                    Index.dataList["0"] = data;

                    var columnInfo = [
                              { caption: '仓库单位', name: 'FHappenCompanyNO'},
                              { caption: '仓库单位名称', name: 'FHappenCompanyName'},
                              { caption: '容器编号', name: 'FContainerNO'},
                              { caption: '容器名称', name: 'FContainerName'},
                              { caption: '重箱<br/>库存', name: 'ZXKC'},
                              { caption: '空箱<br/>库存', name: 'KXKC'},
                              { caption: '库存<br/>数量', name: 'KC'},
                              { caption: '期初<br/>数量', name: 'FQuantity_QC'},
                              { caption: '入库<br/>数量', name: 'FQuantity_RK'},
                              { caption: '出库<br/>数量', name: 'FQuantity_CK'},
                              { caption: '废品<br/>数量', name: 'FQuantity_BS'},
                              { caption: '返修<br/>数量', name: 'FQuantity_FX'},
                              { caption: '盈亏<br/>数量', name: 'FQuantity_PC'}
                    ];

                    var title = Index.titlies[0].format(new Date().format("yyyy年MM月"))

                    Index.renderPrintData($("#print1"), data.rows, columnInfo, title);
                }
            });

            $gridList1.jqGrid('setFrozenColumns');

            $gridList1.jqGrid('setLabel', 0, '序号', 'rowNumberHeaderStyle');

            var conn2 = $.buildPostParam("FHappenCompanyNO,FContainerNO", "2");

            var $gridList2 = $("#gridList2");
            $gridList2.dataGridForJson({
                url: "/WareHouse/Container/GetFlowReceiveAndDispatchExchange",
                postData: conn2,
                height: $(window).height() - 220,
                colModel: [
                 { label: '发生单位', name: 'FHappenCompanyNO', width: 80, align: 'left', sortable: false },
                { label: '发生单位名称', name: 'FHappenCompanyName', width: 140, align: 'left', sortable: false },
                { label: '容器分类编码(隐藏)', name: 'FContainerTypeNO', width: 40, align: 'center', sortable: false, hidden: true },
                { label: '容器分类名称', name: 'FContainerTypeName', width: 140, align: 'left', sortable: false },
                { label: '容器编号', name: 'FContainerNO', width: 100, align: 'center', sortable: false },
                { label: '容器名称', name: 'FContainerName', width: 160, align: 'center', sortable: false },
                { label: '库存<br/>数量', name: 'KCZS', width: 80, align: 'right', sortable: false },
                { label: '期初<br/>数量', name: 'QC', width: 80, align: 'right', sortable: false },
                { label: '空箱<br/>入库', name: 'KRK', width: 60, align: 'right', sortable: false },
                { label: '空箱<br/>出库', name: 'KCK', width: 60, align: 'right', sortable: false },
                { label: '空箱<br/>报废', name: 'KBS', width: 60, align: 'right', sortable: false, },
                { label: '空箱<br/>返修', name: 'KFX', width: 60, align: 'right', sortable: false, },
                { label: '空箱<br/>盘存', name: 'KPC', width: 60, align: 'right', sortable: false },
               { label: '空箱<br/>库存', name: 'KKC', width: 60, align: 'right', sortable: false, },
                { label: '重箱<br/>入库', name: 'ZRK', width: 60, align: 'right', sortable: false, },
                { label: '重箱<br/>出库', name: 'ZCK', width: 60, align: 'right', sortable: false },
                { label: '重箱<br/>库存', name: 'ZKC', width: 60, align: 'right', sortable: false },
                 { label: '', name: '', width: $(window).width() - 1400, align: 'left' }
                ],
                pager: "#gridPager2",
                sortname: 'FHappenCompanyNO,FContainerTypeNO,FContainerNO',
                sortorder: "asc,asc,asc",
                viewrecords: true,
                rownumWidth: 50,
                onSelectRow: function (rowId) {
                    rowData = $gridList2.jqGrid('getRowData', rowId);
                    index = rowId;
                    $.each(rowData, function (key, value) {
                        $("#txt" + key).val(value);
                    });
                },
                loadComplete: function (data) {
                    Index.dataList["1"] = data;

                    var columnInfo = [
                               { caption: '发生单位', name: 'FHappenCompanyNO' },
                               { caption: '发生单位名称', name: 'FHappenCompanyName' },
                               { caption: '容器分类名称', name: 'FContainerTypeName' },
                               { caption: '容器编号', name: 'FContainerNO' },
                               { caption: '容器名称', name: 'FContainerName' },
                               { caption: '库存<br/>数量', name: 'KCZS' },
                               { caption: '期初<br/>数量', name: 'QC' },
                               { caption: '空箱<br/>入库', name: 'KRK' },
                               { caption: '空箱<br/>出库', name: 'KCK' },
                               { caption: '空箱<br/>报废', name: 'KBS' },
                               { caption: '空箱<br/>返修', name: 'KFX' },
                               { caption: '空箱<br/>盘存', name: 'KPC' },
                               { caption: '空箱<br/>库存', name: 'KKC' },
                               { caption: '重箱<br/>入库', name: 'ZRK' },
                               { caption: '重箱<br/>出库', name: 'ZCK' },
                               { caption: '重箱<br/>库存', name: 'ZKC' }
                    ];

                    var title = Index.titlies[0].format(new Date().format("yyyy年MM月"))

                    Index.renderPrintData($("#print2"), data.rows, columnInfo, title);
                }
            });
            $gridList2.jqGrid('setFrozenColumns');

            $gridList2.jqGrid('setLabel', 0, '序号', 'rowNumberHeaderStyle');

        },
        renderPrintData: function (c, data, colunmInfo, title) {
            c.empty();

            var rows = 16;

            var max = data.length;

            var item = { page: 1, maxPage: 1, title: title, unit: Index.curUnit, user: Index.curUserName, date: Index.curDate, time: Index.curTime, table: null };

            var maxPage = Math.ceil(max / rows);

            item.maxPage = maxPage;

            for (var i = 0; i < maxPage; i++) {
                var isFirst = true;

                var start = i * rows, end = (i + 1) * rows;

                end > max && (end = max);

                var table = $("<table>"), pageItem = $("<div>").addClass("page");

                item.page = i + 1;

                for (var j = start; j < end; j++) {
                    var cur = data[j];

                    isFirst && (function () {
                        var title = $("<tr>");

                        for (var idx in colunmInfo) {
                            $("<th>").html(colunmInfo[idx].caption).appendTo(title);
                        }

                        title.appendTo(table);

                        isFirst = false;
                    })();

                    !isFirst && (function () {
                        var row = $("<tr>");

                        for (var idx in colunmInfo) {
                            $("<td>").text(cur[colunmInfo[idx].name]).appendTo(row);
                        }

                        row.appendTo(table);
                    })();
                }

                item.table = "<table>{0}</table>".format(table.html());

                pageItem.html(Index.printItemTemplate.format(item)).appendTo(c);
            }
        },
        buildSql: function (pars, suffix, colInfo, tableName) {
            var param = { sql: "" }, conn = "", cols = [];

            !suffix && (suffix = "");

            param.sql = "SELECT {2} FROM {3} where useunit = '{1}' {0}";

            pars.forEach(function (item) {
                var val = $("#{0}{1}".format(item, suffix)).val();

                !!val && (conn += " and {0} = '{1}'".format(item, val));
            });

            colInfo.forEach(function (item) {
                cols.push("{name} {caption}".format(item));
            });

            param.sql = param.sql.format(conn, Index.curUnit, cols.join(","), tableName);

            console.log(param.sql);

            return param;
        }
    };
</script>


<div class="layout" id="layout" style="width: 100%;">
    <ul id="myTab" class="nav nav-tabs" style="margin: 5px 0 0 0">
        <li class="active"><a href="#FlowReceiveAndDispatchStatisticsReport" data-toggle="tab">流向收发存统计表</a></li>
        <li><a href="#FlowReceiveAndDispatchExchangeReport" data-toggle="tab">流向收发存汇总表</a></li>
    </ul>
    <div id="myTabContent" class="tab-content">
        <div class="tab-pane fade in active" id="FlowReceiveAndDispatchStatisticsReport">
            <div class="tool_c">
                <div class="btn-group  input-width-185">
                    &nbsp;台账日期<br />
                    <input id="FAccountDate1" name="date" type="text" class="txt_common form-control" date-control />
                </div>
                <div class="btn-group input-width-185">
                    &nbsp;仓库单位<br />
                    <select id="FHappenCompanyNO1" name="" class="form-control FHappenCompanyNO">
                        <option value="">==请选择==</option>
                    </select>
                </div>
                <div class="btn-group input-width-185">
                    &nbsp;容器编号<br />
                    <input id="FContainerNO1" name="" placeholder="" type="text" class="form-control required" />
                </div>
                <div class="btn-group">
                    <a id="btn_search1" class="btn btn-primary center-block"><span class="glyphicon glyphicon-search"></span>查询</a>
                    <a class="btn btn-primary center-block" onclick="$.reload()"><span class="glyphicon glyphicon-refresh"></span>&nbsp刷新</a>
                    <a id="butnPrint1" class="btn btn-primary center-block"><span class="glyphicon glyphicon-print"></span>&nbsp打印</a>
                    <a id="butnExport1" class="btn btn-primary center-block"><span class="glyphicon glyphicon-download"></span>&nbsp导出</a>
                </div>
            </div>
            <div class="grid_list_1_c" style="margin-top: 5px;">
                <table id="gridList1"></table>
                <div id="gridPager1"></div>
            </div>
            <div id="print1" class="print">
                <div class="page break_page">
                    <h1>{title}</h1>
                    <div class="top">
                        单位：<div class="unit">{unit}</div>
                        <div class="right_page">第{page}页，共{maxPage}页</div>
                    </div>
                    <div class="table_c">
                        {table}
                    </div>
                    <footer>
                        <span>打印人员：<label>{user}</label></span> <span>打印日期：<label>{date}</label><label>{time}</label></span>
                    </footer>
                </div>
            </div>
        </div>
        <div class="tab-pane fade" id="FlowReceiveAndDispatchExchangeReport">
            <div class="tool_c">
                <div class="btn-group  input-width-185">
                    &nbsp;发生单位<br />
                    <select id="FHappenCompanyNO2" name="" class="form-control FHappenCompanyNO">
                        <option value="">==请选择==</option>
                    </select>
                </div>
                <div class="btn-group input-width-185">
                    &nbsp;容器编号<br />
                    <input id="FDocuNO2" name="" placeholder="" type="text" class="form-control required" />
                </div>
                <div class="btn-group">
                    <a id="btn_search2" class="btn btn-primary center-block"><span class="glyphicon glyphicon-search"></span>查询</a>
                    <a class="btn btn-primary center-block" onclick="$.reload()"><span class="glyphicon glyphicon-refresh"></span>&nbsp刷新</a>
                    <a id="butnPrint2" class="btn btn-primary center-block"><span class="glyphicon glyphicon-print"></span>&nbsp打印</a>
                    <a id="butnExport2" class="btn btn-primary center-block" onclick="btn_Excel()"><span class="glyphicon glyphicon-download"></span>&nbsp导出</a>
                </div>
            </div>
            <div class="grid_list_2_c" style="margin-top: 5px; display: block;">
                <table id="gridList2" style="width: 100%;"></table>
                <div id="gridPager2"></div>
            </div>
            <div id="print2" class="print">
                <div class="page break_page">
                    <h1>{title}</h1>
                    <div class="top">
                        单位：<div class="unit">{unit}</div>
                        <div class="right_page">第{page}页，共{maxPage}页</div>
                    </div>
                    <div class="table_c">
                        {table}
                    </div>
                    <footer>
                        <span>打印人员：<label>{user}</label></span> <span>打印日期：<label>{date}</label><label>{time}</label></span>
                    </footer>
                </div>
            </div>
        </div>
    </div>
</div>






